package ym.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ym.mapper.RepairAppealMapper;
import ym.pojo.RepairAppeal;
import ym.service.RepairAppealService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class RepairAppealServiceImpl implements RepairAppealService {
    @Autowired
    private RepairAppealMapper repairAppealMapper;

    @Override
    public void add(RepairAppeal repairAppeal) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        String timeNow = sdf.format(new Date());
        repairAppeal.setRepairAppealTime(timeNow);
        repairAppealMapper.insert(repairAppeal);
    }

    @Override
    public void update(RepairAppeal repairAppeal) {
        repairAppealMapper.updateById(repairAppeal);
    }

    @Override
    public void delete(Integer id) {
        repairAppealMapper.deleteById(id);
    }

    @Override
    public RepairAppeal findById(Integer id) {
        return repairAppealMapper.selectById(id);
    }

    @Override
    public List<RepairAppeal> findAll() {
        return repairAppealMapper.selectList(null);
    }

    @Override
    public Page<RepairAppeal> search(String search, int page, int size) {
        QueryWrapper<RepairAppeal> queryWrapper = new QueryWrapper<>();
        if (search != null) {
            // 可以根据实际需求修改查询条件
            // queryWrapper.like("", search);
        }
        return repairAppealMapper.selectPage(new Page<>(page, size), queryWrapper);
    }

    @Override
    public Page<RepairAppeal> searchByRepairReportId(Integer repairReportId, String search, int page, int size) {
        QueryWrapper<RepairAppeal> queryWrapper = new QueryWrapper<>();
        if (search != null) {
            queryWrapper.eq("repairReportId", repairReportId);
        }else {
            queryWrapper.eq("repairReportId", repairReportId);
        }
        return repairAppealMapper.selectPage(new Page<>(page, size), queryWrapper);
    }
}
